<?php
!defined('EASYDER') && exit('Forbidden');

class EUpload {

	//获取文件后缀名函数
    function getFileExt($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
   //生成随机文件名函数

    function getRandomName($length)
    {
       $hash = 'CR-';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
            for($i = 0; $i < $length; $i++)
            {
                $hash .= $chars[mt_rand(0, $max)];
            }
			//$hash=date(ymdHis);
        return $hash=md5(time().date("YmdHis"));
    }
	function upload($name)
	{
		 $uploaddir = "./data/uploads";//设置文件保存目录 注意包含/
  		 $type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型
   		 $patch="./";//程序所在路径
   		 $pa=$_SERVER['SCRIPT_FILENAME'];
   		 $url=substr($pa, 0,strripos($pa,'/')+1);
   		 $p=$url.$uploaddir;
   		 @mkdir($p);
		 $a=strtolower(fileextLIST($name['name']));
		 //判断文件类型
		 if(!in_array(strtolower(fileextLIST($name['name'])),$type))
		 {
			$text=implode(",",$type);
			//echo "您只能上传以下类型文件: ",$text,"<br>";
		 }
	   //生成目标文件的文件名
	   else{
			$filename=explode(".",$name['name']);
			do
			{
				$filename[0]=randomLIST(10); //设置随机数长度
				$name=implode(".",$filename);
				$uploadfile=$uploaddir.'/'.$name;
			}
		   while(file_exists($uploadfile));
				if(is_uploaded_file($name['tmp_name'])){

					if (move_uploaded_file($name['tmp_name'],$uploadfile)){
						$file_name;
						if($filename){
							$i=0;
							while($f=each($filename))
							{
									if($i==0)$file_name.=$f['value'];
									else $file_name.='.'.$f['value'];
									$i++;
							}
						}
						return substr($uploadfile,2);


					}
					else{
							return "上传失败！";
					}
			   }
		  }
	}
	
}
?>